package com.example.weight.greenSeasons.flocculation;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import net.cachapa.expandablelayout.ExpandableLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class xuningshebeijiancha_old extends AppCompatActivity{
    private String username, factory;
    private ExpandableLayout expand_1;
    private ExpandableLayout expand_2;
    private Button textView1;
    private Button textView2;
    private TextView dateEdit;
    private TitleBar titleBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent previousIntent = getIntent();
        username = previousIntent.getStringExtra("name");
        factory = previousIntent.getStringExtra("factory");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuningshebeijiancha_old);
        expand_1=findViewById(R.id.expandable_1);
        expand_2=findViewById(R.id.expandable_2);
        textView1=findViewById(R.id.button_1);
        textView2=findViewById(R.id.button_2);

        dateEdit=findViewById(R.id.compost_editDate);
        Calendar calendar=Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH)+1; //第一个月从0开始，所以得到月份＋1
        Integer day = calendar.get(calendar.DAY_OF_MONTH);
        Integer hour24 = calendar.get(calendar.HOUR_OF_DAY);
        Integer minute = calendar.get(calendar.MINUTE);
        dateEdit.setText(year+"-"+month+"-"+day);

        TextView xuning_equipment_examain_1  = findViewById(R.id.xuning_equipment_examain_1 );
        TextView xuning_equipment_examain_2  = findViewById(R.id.xuning_equipment_examain_2 );
        TextView xuning_equipment_examain_3  = findViewById(R.id.xuning_equipment_examain_3 );
        TextView xuning_equipment_examain_4  = findViewById(R.id.xuning_equipment_examain_4 );
        TextView xuning_equipment_examain_5  = findViewById(R.id.xuning_equipment_examain_5 );
        TextView xuning_equipment_examain_6  = findViewById(R.id.xuning_equipment_examain_6 );
        TextView xuning_equipment_examain_7  = findViewById(R.id.xuning_equipment_examain_7 );
        TextView xuning_equipment_examain_8  = findViewById(R.id.xuning_equipment_examain_8 );
        TextView xuning_equipment_examain_9  = findViewById(R.id.xuning_equipment_examain_9 );
        TextView xuning_equipment_examain_10 = findViewById(R.id.xuning_equipment_examain_10);

        RadioGroup radioGroup1  = findViewById(R.id.equipment1_group_button);
        RadioGroup radioGroup2  = findViewById(R.id.equipment2_group_button);
        RadioGroup radioGroup3  = findViewById(R.id.equipment3_group_button);
        RadioGroup radioGroup4  = findViewById(R.id.equipment4_group_button);
        RadioGroup radioGroup5  = findViewById(R.id.equipment5_group_button);
        RadioGroup radioGroup6  = findViewById(R.id.equipment6_group_button);
        RadioGroup radioGroup7  = findViewById(R.id.equipment7_group_button);
        RadioGroup radioGroup8  = findViewById(R.id.equipment8_group_button);
        RadioGroup radioGroup9  = findViewById(R.id.equipment9_group_button);
        RadioGroup radioGroup10 = findViewById(R.id.equipment10_group_button);

        RadioButton radioGroup1_normal = findViewById(R.id.equipment1_normal_button);
        RadioButton radioGroup2_normal = findViewById(R.id.equipment2_normal_button);
        RadioButton radioGroup3_normal = findViewById(R.id.equipment3_normal_button);
        RadioButton radioGroup4_normal = findViewById(R.id.equipment4_normal_button);
        RadioButton radioGroup5_normal = findViewById(R.id.equipment5_normal_button);
        RadioButton radioGroup6_normal = findViewById(R.id.equipment6_normal_button);
        RadioButton radioGroup7_normal = findViewById(R.id.equipment7_normal_button);
        RadioButton radioGroup8_normal = findViewById(R.id.equipment8_normal_button);
        RadioButton radioGroup9_normal = findViewById(R.id.equipment9_normal_button);
        RadioButton radioGroup10_normal = findViewById(R.id.equipment10_normal_button);

        RadioButton radioGroup1_abnormal = findViewById(R.id.equipment1_abnormal_button);
        RadioButton radioGroup2_abnormal = findViewById(R.id.equipment2_abnormal_button);
        RadioButton radioGroup3_abnormal = findViewById(R.id.equipment3_abnormal_button);
        RadioButton radioGroup4_abnormal = findViewById(R.id.equipment4_abnormal_button);
        RadioButton radioGroup5_abnormal = findViewById(R.id.equipment5_abnormal_button);
        RadioButton radioGroup6_abnormal = findViewById(R.id.equipment6_abnormal_button);
        RadioButton radioGroup7_abnormal = findViewById(R.id.equipment7_abnormal_button);
        RadioButton radioGroup8_abnormal = findViewById(R.id.equipment8_abnormal_button);
        RadioButton radioGroup9_abnormal = findViewById(R.id.equipment9_abnormal_button);
        RadioButton radioGroup10_abnormal = findViewById(R.id.equipment10_abnormal_button);

        titleBar=findViewById(R.id.titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                if (radioGroup1.getCheckedRadioButtonId() == -1) {
                    allDone("大调节池液位计");
                }else if (radioGroup2.getCheckedRadioButtonId() == -1) {
                    allDone("大调节池搅拌器");
                }else if (radioGroup3.getCheckedRadioButtonId() == -1) {
                    allDone("过滤器");
                }else if (radioGroup4.getCheckedRadioButtonId() == -1) {
                    allDone("水洗喷嘴");
                }else if (radioGroup5.getCheckedRadioButtonId() == -1) {
                    allDone("喷淋滤嘴");
                }else if (radioGroup6.getCheckedRadioButtonId() == -1) {
                    allDone("使用液药箱探头");
                }else if (radioGroup7.getCheckedRadioButtonId() == -1) {
                    allDone("粉剂螺旋");
                }else if (radioGroup8.getCheckedRadioButtonId() == -1) {
                    allDone("1# 搅拌器");
                }else if (radioGroup9.getCheckedRadioButtonId() == -1) {
                    allDone("2# 搅拌器");
                }else if (radioGroup10.getCheckedRadioButtonId() == -1) {
                    allDone("上水系统");
                }else {
                    xuningshebeijiancha_old.this.finish();
                }
            }
            @Override
            public void onTitleClick(View v) { }
            @Override
            public void onRightClick(View v) { }
        });
        //equip_1
        radioGroup1_normal.setOnClickListener(v -> {
            if (radioGroup1_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_1.getText().toString(), radioGroup1_normal, radioGroup1_abnormal,radioGroup1);
            }
        });
        radioGroup1_abnormal.setOnClickListener(v -> {
            if (radioGroup1_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_1.getText().toString(), radioGroup1_abnormal, radioGroup1_abnormal,radioGroup1);
            }
        });
        //equip_2
        radioGroup2_normal.setOnClickListener(v -> {
            if (radioGroup2_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_2.getText().toString(), radioGroup2_normal, radioGroup2_abnormal,radioGroup2);
            }
        });
        radioGroup2_abnormal.setOnClickListener(v -> {
            if (radioGroup2_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_2.getText().toString(), radioGroup2_abnormal, radioGroup2_abnormal,radioGroup2);
            }
        });
        //equip_3
        radioGroup3_normal.setOnClickListener(v -> {
            if (radioGroup3_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_3.getText().toString(), radioGroup3_normal, radioGroup3_abnormal,radioGroup3);
            }
        });
        radioGroup3_abnormal.setOnClickListener(v -> {
            if (radioGroup3_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_3.getText().toString(), radioGroup3_abnormal, radioGroup3_abnormal,radioGroup3);
            }
        });

        //equip_4
        radioGroup4_normal.setOnClickListener(v -> {
            if (radioGroup4_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_4.getText().toString(), radioGroup4_normal, radioGroup4_abnormal,radioGroup4);
            }
        });
        radioGroup4_abnormal.setOnClickListener(v -> {
            if (radioGroup4_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_4.getText().toString(), radioGroup4_abnormal, radioGroup4_abnormal,radioGroup4);
            }
        });

        //equip_5
        radioGroup5_normal.setOnClickListener(v -> {
            if (radioGroup5_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_5.getText().toString(), radioGroup5_normal, radioGroup5_abnormal,radioGroup5);
            }
        });
        radioGroup5_abnormal.setOnClickListener(v -> {
            if (radioGroup5_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_5.getText().toString(), radioGroup5_abnormal, radioGroup5_abnormal,radioGroup5);
            }
        });

        //equip_6
        radioGroup6_normal.setOnClickListener(v -> {
            if (radioGroup6_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_6.getText().toString(), radioGroup6_normal, radioGroup6_abnormal,radioGroup6);
            }
        });
        radioGroup6_abnormal.setOnClickListener(v -> {
            if (radioGroup6_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_6.getText().toString(), radioGroup6_abnormal, radioGroup6_abnormal,radioGroup6);
            }
        });

        //equip_7
        radioGroup7_normal.setOnClickListener(v -> {
            if (radioGroup7_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_7.getText().toString(), radioGroup7_normal, radioGroup7_abnormal,radioGroup7);
            }
        });
        radioGroup7_abnormal.setOnClickListener(v -> {
            if (radioGroup7_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_7.getText().toString(), radioGroup7_abnormal, radioGroup7_abnormal,radioGroup7);
            }
        });

        //equip_8
        radioGroup8_normal.setOnClickListener(v -> {
            if (radioGroup8_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_8.getText().toString(), radioGroup8_normal, radioGroup8_abnormal,radioGroup8);
            }
        });
        radioGroup8_abnormal.setOnClickListener(v -> {
            if (radioGroup8_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_8.getText().toString(), radioGroup8_abnormal, radioGroup8_abnormal,radioGroup8);
            }
        });

        //equip_9
        radioGroup9_normal.setOnClickListener(v -> {
            if (radioGroup9_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_9.getText().toString(), radioGroup9_normal, radioGroup9_abnormal,radioGroup9);
            }
        });
        radioGroup9_abnormal.setOnClickListener(v -> {
            if (radioGroup9_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_9.getText().toString(), radioGroup9_abnormal, radioGroup9_abnormal,radioGroup9);
            }
        });

        //equip_10
        radioGroup10_normal.setOnClickListener(v -> {
            if (radioGroup10_normal.isChecked()) {
                saveNormal(xuning_equipment_examain_10.getText().toString(), radioGroup10_normal, radioGroup10_abnormal,radioGroup10);
            }
        });
        radioGroup10_abnormal.setOnClickListener(v -> {
            if (radioGroup10_abnormal.isChecked()) {
                saveAbnormal(xuning_equipment_examain_10.getText().toString(), radioGroup10_abnormal, radioGroup10_abnormal,radioGroup10);
            }
        });

        //获取当天最新一条数据
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/sjq/flocculationProduceCheck/findEquipList" + "?factory=" + factory;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            Log.d("jy", response);
                            JSONArray data;
                            if(jsonObject.get("data")!=null && !jsonObject.get("data").toString().equals("null")) {
                                data = jsonObject.getJSONArray("data");
                                if(data.length()>0){
                                    for(int i=0;i<data.length();i++){
                                        // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                                        JSONObject record = data.getJSONObject(i);
                                        // 得到 每个对象中的属性值
                                        if (record.get("equipmentName").equals("大调节池液位计")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment1_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment1_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup1_normal.setClickable(false);
                                            radioGroup1_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("大调节池搅拌器")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment2_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment2_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup2_normal.setClickable(false);
                                            radioGroup2_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("过滤器")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment3_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment3_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup3_normal.setClickable(false);
                                            radioGroup3_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("水洗喷嘴")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment4_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment4_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup4_normal.setClickable(false);
                                            radioGroup4_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("喷淋滤嘴")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment5_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment5_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup5_normal.setClickable(false);
                                            radioGroup5_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("使用液药箱探头")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment6_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment6_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup6_normal.setClickable(false);
                                            radioGroup6_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("粉剂螺旋")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment7_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment7_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup7_normal.setClickable(false);
                                            radioGroup7_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("1# 搅拌器")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment8_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment8_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup8_normal.setClickable(false);
                                            radioGroup8_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("2# 搅拌器")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment9_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment9_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup9_normal.setClickable(false);
                                            radioGroup9_abnormal.setClickable(false);
                                        }else if (record.get("equipmentName").equals("上水系统")){
                                            if(record.get("isNormal").equals("正常")){
                                                RadioButton btn = findViewById(R.id.equipment10_normal_button);
                                                btn.setChecked(true);
                                            }else {
                                                RadioButton btn = findViewById(R.id.equipment10_abnormal_button);
                                                btn.setChecked(true);
                                            }
                                            radioGroup10_normal.setClickable(false);
                                            radioGroup10_abnormal.setClickable(false);
                                        }
                                    }
                                }
                            }
//                            radioGroup1.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("大调节池液位计", button);
//                                }else {
//                                    saveAbnormal("大调节池液位计", button);
//                                }
//
//                            });
//                            radioGroup2.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("大调节池搅拌器", button);
//                                }else {
//                                    saveAbnormal("大调节池搅拌器", button);
//                                }
//
//                            });
//                            radioGroup3.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("过滤器", button);
//                                }else {
//                                    saveAbnormal("过滤器", button);
//                                }
//
//                            });
//                            radioGroup4.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("水洗喷嘴", button);
//                                }else {
//                                    saveAbnormal("水洗喷嘴", button);
//                                }
//
//                            });
//                            radioGroup5.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("喷淋滤嘴", button);
//                                }else {
//                                    saveAbnormal("喷淋滤嘴", button);
//                                }
//
//                            });
//                            radioGroup6.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("使用液药箱探头", button);
//                                }else {
//                                    saveAbnormal("使用液药箱探头", button);
//                                }
//
//                            });
//                            radioGroup7.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("粉剂螺旋", button);
//                                }else {
//                                    saveAbnormal("粉剂螺旋", button);
//                                }
//
//                            });
//                            radioGroup8.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("1# 搅拌器", button);
//                                }else {
//                                    saveAbnormal("1# 搅拌器", button);
//                                }
//
//                            });
//                            radioGroup9.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("2# 搅拌器", button);
//                                }else {
//                                    saveAbnormal("2# 搅拌器", button);
//                                }
//
//                            });
//                            radioGroup10.setOnCheckedChangeListener((group, checkedId) -> {
//                                RadioButton button = findViewById(checkedId);
//                                String isNormal = button.getText().toString();
//                                if (isNormal.equals("正常")){
//                                    saveNormal("上水系统", button);
//                                }else {
//                                    saveAbnormal("上水系统", button);
//                                }
//
//                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(xuningshebeijiancha_old.this,
                        "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);
    }

    public void show1(View view) {
        if (expand_1.isExpanded()){
            textView1.setText("+ 除臭系统");
        }else{
            textView1.setText("- 除臭系统");
        }

        expand_1.toggle();
    }
    public void show2(View view) {
        if (expand_2.isExpanded()){
            textView2.setText("+ 药剂装置");
        }else{
            textView2.setText("- 药剂装置");
        }

        expand_2.toggle();
    }
    public void saveNormal(String equipment_name, RadioButton normal_rb, RadioButton abnormal_rb,RadioGroup rb) {
        new AlertDialog.Builder(this)
                .setTitle("确定 “"+ equipment_name +"” 正常？")
                .setCancelable(false)
                .setNegativeButton("否", ((dialog, which) -> rb.clearCheck()))
                .setPositiveButton("是", (dialog, which) ->{
                    // 提交表单
                    RequestQueue queue = Volley.newRequestQueue(this);
                    String url = Config.url + "/sjq/flocculationProduceCheck/addOperationalForm";

                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    Log.d("zys", response);
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String code = jsonObject.getString("code");
                                        if (code.equals("200")) {
                                            normal_rb.setClickable(false);
                                            abnormal_rb.setClickable(false);
                                            Toast.makeText(xuningshebeijiancha_old.this,
                                                    "保存成功", Toast.LENGTH_SHORT).show();
                                        } else
                                            Toast.makeText(xuningshebeijiancha_old.this,
                                                    "保存失败", Toast.LENGTH_SHORT).show();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(xuningshebeijiancha_old.this,
                                    "保存失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> map=new HashMap<>();
                            map.put("equipment_name", equipment_name);
                            map.put("is_normal", "正常");
                            map.put("remarks", "");
                            map.put("operator", username);
                            map.put("factory", factory);
                            return map;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    queue.add(stringRequest);
                })
                .show();
    }

    public void saveAbnormal(String equipment_name, RadioButton normal_rb, RadioButton abnormal_rb,RadioGroup rb) {
        final EditText equipment_problem = new EditText(this);
        equipment_problem.setHint("   输入问题:");
        new AlertDialog.Builder(this)
                .setTitle("请填写 “"+ equipment_name +"” 异常原因？")
                .setView(equipment_problem)
                .setCancelable(false)
                .setNegativeButton("取消", ((dialog, which) -> rb.clearCheck()))
                .setPositiveButton("提交", (dialog, which) ->{
                    // 提交表单
                    RequestQueue queue = Volley.newRequestQueue(this);
                    String url = Config.url + "/sjq/flocculationProduceCheck/addOperationalForm";

                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    Log.d("jy", response);
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String code = jsonObject.getString("code");
                                        if (code.equals("200")){
                                            normal_rb.setClickable(false);
                                            abnormal_rb.setClickable(false);
                                            Toast.makeText(xuningshebeijiancha_old.this,
                                                    "保存成功", Toast.LENGTH_SHORT).show();
                                        } else
                                            Toast.makeText(xuningshebeijiancha_old.this,
                                                    "保存失败", Toast.LENGTH_SHORT).show();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(xuningshebeijiancha_old.this,
                                    "保存失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> map=new HashMap<>();
                            map.put("equipment_name", equipment_name);
                            map.put("is_normal", "异常");
                            map.put("remarks", equipment_problem.getText().toString());
                            map.put("operator", username);
                            map.put("factory", factory);
                            return map;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    queue.add(stringRequest);
                })
                .show();
    }
    public void allDone(String equipment_name){
        new AlertDialog.Builder(this)
                .setTitle(" “"+ equipment_name +"” 未检查，是否退出")
                .setCancelable(false)
                .setNegativeButton("否", ((dialog, which) -> {
                    return;
                }))
                .setPositiveButton("是", (dialog, which) ->{
                    xuningshebeijiancha_old.this.finish();
                })
                .show();
    }
}